﻿package com.ouken.activity;





import device_ctr.Dev_logic;
import device_ctr.Wifi_ctronller;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class AddDeviceInputPWDActivity extends Activity {
	private Context context;
	private ImageButton back2;
	private ImageButton back1;
	private EditText text1;
	private ImageButton button1;
	private String devicePWD;
	private String deviceSSID;
	private String deviceBSSID;
	private int deviceName;
	
	private void initView(){
		context = this;
		back1 = (ImageButton)findViewById(R.id.add_device_input_pwd_back1); 
		back2 = (ImageButton)findViewById(R.id.add_device_input_pwd_back2); 
		text1 = (EditText)findViewById(R.id.add_device_input_pwd_text1);
		button1 = (ImageButton)findViewById(R.id.add_device_input_pwd_button1); 
		
		Intent intent = getIntent(); 
		devicePWD = intent.getStringExtra("devicePWD").toString();
		deviceName = intent.getIntExtra("deviceName", 0);
		deviceSSID=intent.getStringExtra("deviceSSID").toString();
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_device_input_pwd);
		initView();
		
		back1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AddDeviceInputPWDActivity.this.finish();
			}
		});
		
		back2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AddDeviceInputPWDActivity.this.finish();
			}
		});
		
		button1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String checkResult = "OK";
		        int errorMessage = 0  ;
		        if(text1.getText().length() == 0){
		        	checkResult = "ERROR";
		        	errorMessage = R.string.passwdempty;
		        }
		        else {
		        	if (text1.getText().toString().matches("[0-9A-Za-z]*")==true && text1.getText().toString().length()<9){
			        	Dev_logic logic_er=new Dev_logic();
				        if (logic_er.check_pw(text1.getText().toString())==false){
				        	checkResult = "ERROR";
				        	errorMessage = R.string.passwd_uncorrect;
				        }
				        
				        //BSSID信息獲得
				        Wifi_ctronller wifi_reader=new Wifi_ctronller(context);
				        deviceBSSID= wifi_reader.getBSSID();
		        	}
		        	else{
		        		checkResult = "ERROR";
			        	errorMessage = R.string.passwd_format_error;
		        	}

		        }
		        if(checkResult.equals("ERROR")){
		        	AlertDialog myDialog = new AlertDialog.Builder(AddDeviceInputPWDActivity.this).
		        	setTitle(R.string.error).
		        	setMessage(errorMessage).
		        	setPositiveButton(R.string.confirm, null).create();
		        	myDialog.show();
					return;
		        }
		        devicePWD=text1.getText().toString();
		        Intent intent ;
		        if(deviceName == R.string.device_name_01){
		           intent = new Intent(context, AddDevice01Activity.class); 
		        }else if(deviceName == R.string.device_name_02){
			           intent = new Intent(context, AddDevice02Activity.class); 
			    }else if(deviceName == R.string.device_name_03){
			           intent = new Intent(context, AddDevice03Activity.class); 
			    }else if(deviceName == R.string.device_name_04){
			           intent = new Intent(context, AddDevice04Activity.class); 
			    }else if(deviceName == R.string.device_name_05){
			           intent = new Intent(context, AddDevice05Activity.class); 
			    }else if(deviceName == R.string.device_name_06){
			           intent = new Intent(context, AddDevice06Activity.class); 
			    }else if(deviceName == R.string.device_name_07){
			           intent = new Intent(context, AddDevice07Activity.class); 
			    }else if(deviceName == R.string.device_name_08){
			           intent = new Intent(context, AddDevice08Activity.class); 
			    }else if(deviceName == R.string.device_name_09){
			           intent = new Intent(context, AddDevice09Activity.class); 
			    }else if(deviceName == R.string.device_name_10){
			           intent = new Intent(context, AddDevice10Activity.class); 
			    }
			    else if(deviceName == R.string.device_name_11){
			           intent = new Intent(context, AddDevice11Activity.class); 
			    }
			    else if(deviceName == R.string.device_name_12){
			           intent = new Intent(context, AddDevice12Activity.class); 
			    }
			    else if(deviceName == R.string.device_name_13){
			           intent = new Intent(context, AddDevice13Activity.class); 
			    }
			    else if(deviceName == R.string.device_name_14){
			           intent = new Intent(context, AddDevice14Activity.class); 
			    }
			    else return;
		        
		        intent.putExtra("deviceBSSID",deviceBSSID.toString());
		        intent.putExtra("deviceSSID",deviceSSID.toString());
		        intent.putExtra("devicePWD",devicePWD.toString());
		        
		        startActivity(intent);
		        AddDeviceInputPWDActivity.this.finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}
}
